package gatech.cs2340.adventuretime.persistent.records;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import gatech.cs2340.adventuretime.persistent.users.*;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Predicate;

import junit.framework.TestCase;


public class recordsTest extends TestCase {
	User user;
	Appointment appointment;
	Diagnosis tr;
	Patient patient;
	Doctor doctor;	
	Invoice invoice;

	
	@SuppressWarnings("unchecked")
	public void testDataStore(){
		/**
		 * This will also be a test run of our db4o setup
		 * MY FIRST DATABASE WRITE YAYYYYYYYY
		 */
		EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
		config.common().objectClass("gatech.cs2340.adventuretime.persistent.users.Patient").cascadeOnUpdate(true);
		config.common().objectClass("gatech.cs2340.adventuretime.persistent.records.TreatmentRecord").cascadeOnUpdate(true);
		config.common().objectClass("gatech.cs2340.adventuretime.persistent.records.Prescription").cascadeOnUpdate(true);
		config.common().objectClass("gatech.cs2340.adventuretime.persistent.users.Doctor").cascadeOnUpdate(true);

		ObjectContainer db = Db4oEmbedded.openFile(config,"testDB4O2");
		
		patient = new Patient("blin.bladeau@gmail.com","password");
		doctor = new Doctor("whillyGEE@gmail.com","password");
		
		
		ArrayList<Prescription> pres = new ArrayList<Prescription>();
		Prescription pA = new Prescription("whut",90,"take it nonstop");
		Prescription pB = new Prescription("nuthin",100,"take as suppository twice daily");
		pres.add(pA);
		pres.add(pB);
		Diagnosis tr = new Diagnosis(doctor,patient);
		tr.setPrescriptions(pres);
		ObjectSet<Prescription> results;
		try{
			db.store(tr);
			Prescription prototype = new Prescription(null,0,null);
			/* Encountered massive DB4O operational mindsplosion. Update depth. */
			results = db.queryByExample(prototype);
			System.out.println(results.toString());
		}finally{
			db.close();
		}
		//Prescription pFinal = results.next();
		//assertEquals(pFinal,pA);
		//System.out.println(results.toString());
		
	}
//	public void testCRUD(){
//		patient = new Patient("blin.bladeau@gmail.com","password");
//		doctor = new Doctor("whillyGEE@gmail.com","password");
//		User patientt = new Patient("bob@mac.com","snagglefrits");
//		ObjectSet<? extends User> users;
//		
//		try{
//			db.store(patient);
//			db.store(patientt);
//			db.store(doctor);
//			users = db.queryByExample(new Patient("bob@mac.com",""));
//		}finally{
//			db.close();
//		}
//		
//
//		User found = users.next();
//		assertEquals(found.getUsername(),patient.getUsername());
//	}
	public void testUserRead(){
		
		
		

	}
	public void testUserUpdate(){
		

	}
	public void testUserDelete(){

	}
	public void testAppointmentCreate(){

	}
	public void testAppointmentRead(){
		
	}
	public void testAppointmentUpdate(){
		
	}
	public void testAppointmentDelete(){
		
	}
	public void testTreatmentRecordCreate(){
		
	}
	public void testTreatmentRecordRead(){
		
	}
	@SuppressWarnings({ "serial" })
	private class UsernamePredicate extends Predicate<User>
    {
    	String username;
    	/**
    	 * initializes the query object
    	 * @param username this is the thing this query is build for
    	 */
    	public UsernamePredicate(String username){
    		this.username = username;
    	}
    	public boolean match(User user) {
    		
    		return this.username.equals(user.getUsername());
    	}
    	
    }
	private class UserByTypePredicate extends Predicate<User>
	{
		User user;
		public UserByTypePredicate(User user){
			this.user = user;
		}
		@Override
		public boolean match(User user) {
			// TODO Auto-generated method stub
			return this.getClass().equals(user.getClass());
		}
		
	}
	private class AppointmentsByDatePredicate extends Predicate<Appointment>
    {
    	Date date;
    	/**
    	 * initializes the query object
    	 * @param username this is the thing this query is build for
    	 */
    	public AppointmentsByDatePredicate(Date date){
    		this.date = date;
    	}
		@Override
		public boolean match(Appointment appointment) {
			// TODO Auto-generated method stub
			return appointment.getDate().equals(this.date);
		}
    	
    }private class TreatmentRecordsByDoctorNamePredicate extends Predicate<Diagnosis>
    {
    	Diagnosis tr;
    	/**
    	 * initializes the query object
    	 * @param username this is the thing this query is build for
    	 */
    	public TreatmentRecordsByDoctorNamePredicate(Diagnosis tr){
    		this.tr= tr;
    	}
 
		public boolean match(Diagnosis tr) {
			
			return this.tr.getDoctor().getUsername().equalsIgnoreCase(tr.getDoctor().getUsername());
		}
    	
    }private class InvoiceByDayPredicate extends Predicate<Invoice>
    {
    	Invoice invoice;
    	/**
    	 * initializes the query object
    	 * @param username this is the thing this query is build for
    	 */
    	public InvoiceByDayPredicate(Invoice invoice){
    		this.invoice = invoice;
    	}
    	
		@Override
		public boolean match(Invoice invoice) {
			//if the invoice's two times are within 12 hrs of each other....
			if((this.invoice.getDate().getTime() > (invoice.getDate().getTime()-43400000)) || (this.invoice.getDate().getTime() < (invoice.getDate().getTime()+43400000)))
			{return true;}
			return false;
		}
    	
    }
}
